package com.lv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lv.mapper.TeacherMapper;
import com.lv.pojo.LoginFrom;
import com.lv.pojo.Teacher;
import com.lv.service.TeacherService;
import com.lv.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/4/28 19:02
 */
@Service("teacherServiceImpl")
@Transactional
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {


    @Override
    public Teacher login(LoginFrom loginFrom) {
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",loginFrom.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginFrom.getPassword()));
        Teacher teacher= baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public Teacher getTeacherById(long userId) {
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public IPage<Teacher> getTeacherByOpr(Page<Teacher> pageParam, Teacher teacher) {
        QueryWrapper queryWrapper = null;
        if (teacher != null) {
            queryWrapper = new QueryWrapper();

            //班级名称条件
            String clazzName = teacher.getClazzName();
            if (!StringUtils.isEmpty(clazzName)) {
                queryWrapper.eq("clazz_name", clazzName);
            }
            //教师名称条件
            String teacherName = teacher.getName();
            if (!StringUtils.isEmpty(teacherName)) {
                queryWrapper.like("name", teacherName);
            }
            queryWrapper.orderByAsc("id");
            queryWrapper.orderByDesc("name");
        }
        IPage<Teacher> page = baseMapper.selectPage(pageParam,queryWrapper);
        return page;
    }

    /**
     * 修改密码
     * @param userId
     * @param oldPwd
     * @return
     */
    @Override
    public Teacher updatePwd(Long userId, String oldPwd) {
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId.intValue());
        queryWrapper.eq("password",oldPwd);
        Teacher teacher = getOne(queryWrapper);
        return teacher;
    }
}
